﻿/********************************************************************************************
 * Time de desenvolvedores
 *  Vinícius Batista de Souza       @vbs_br
 *  Fermando Martin Figuera         @fernandomartin
 *  
 *  
 * Jogo desenvolvido para testar algumas features e produzir trilha de treinamento no MSDN.
 * Faça o download do SDK do Windows Phone, faça você também o seu jogo :)
 * 
 * Dúvidas utilizem o Forúm do Windows Phone no MSDN.
********************************************************************************************/



using System;
using System.Windows;
using System.Windows.Input;
using Microsoft.Phone.Controls;
using System.Windows.Threading;

namespace ComboBUG
{
    public partial class AboutPage : PhoneApplicationPage
    {
        DispatcherTimer timerScroll;

        private double scrollV = 0;
        private double scrollV_drag_end = 0;
        private double scrollV_drag_begin = 0;
        private int flag_direction = 1;
        double passo = 1;

        public AboutPage()
        {
            InitializeComponent();

            timerScroll = new DispatcherTimer();
            timerScroll.Interval = TimeSpan.FromMilliseconds(100);
            timerScroll.Tick += new EventHandler(timerScroll_Tick);
            timerScroll.Start();
            scrollV = passo;

            scrollViewer1.ManipulationCompleted += new EventHandler<ManipulationCompletedEventArgs>(scrollViewer1_ManipulationCompleted);
            scrollViewer1.ManipulationStarted += new EventHandler<ManipulationStartedEventArgs>(scrollViewer1_ManipulationStarted);

        }

        #region scrollViewer1_ManipulationStarted
        void scrollViewer1_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            scrollV_drag_begin = scrollViewer1.VerticalOffset;
            timerScroll.Stop();
        }
        #endregion


        #region scrollViewer1_ManipulationCompleted
        void scrollViewer1_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
        {
            scrollV_drag_end = scrollViewer1.VerticalOffset;

            if (scrollV_drag_begin < scrollV_drag_end)
                flag_direction = 1;
            else
                flag_direction = 0;


            scrollV = scrollViewer1.VerticalOffset;
            timerScroll.Start();
        }
        #endregion

        #region OnNavigatedFrom
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);

            timerScroll.Stop();
            timerScroll = null;
        }
        #endregion


        #region timerScroll_Tick
        void timerScroll_Tick(object sender, EventArgs e)
        {

            if (flag_direction == 1 && scrollV >= scrollViewer1.ScrollableHeight)
                flag_direction = 0;

            if (flag_direction == 0 && scrollV <= 0)
                flag_direction = 1;

            scrollViewer1.ScrollToVerticalOffset(scrollV);
            scrollViewer1.UpdateLayout();

            if (flag_direction == 1)
                scrollV += passo;
            else
                scrollV -= passo;
        }
        #endregion

        #region Button_Click
        private void Button_Click(object sender, RoutedEventArgs e)
        {

            NavigationService.Navigate(new Uri("/PageBlog.xaml", UriKind.Relative));

            //  NavigationService.Navigate(new Uri(
            //string.Format("/GamePage.xaml?codigo={0}&nome={1}",
            //parametro1, parametro2), UriKind.Relative));


        }
        #endregion
    }
}